﻿using DocumentFormat.OpenXml.Lite.Xml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace DocumentFormat.OpenXml.Lite.SpreadsheetML.Drawing
{
    public class Point : XmlSerializable
    {
        [XmlElement("col", Namespace = Constants.Drawing)]
        public Text<int> Column { get; set; }

        [XmlElement("colOff", Namespace = Constants.Drawing)]
        public Text<long> ColumnOffset { get; set; }

        [XmlElement("row", Namespace = Constants.Drawing)]
        public Text<int> Row { get; set; }

        [XmlElement("rowOff", Namespace = Constants.Drawing)]
        public Text<long> RowOffset { get; set; }

        public Point() { }

        public override void ReadXml(System.Xml.XmlReader reader)
        {
            var r = reader.ReadSubtree();
            while (r.Read())
            {
                if(r.NodeType == System.Xml.XmlNodeType.EndElement)
                    continue;

                switch (r.LocalName)
                {
                    case "col": r.Read();
                        Column = new Text<int>(int.Parse(r.Value));
                        break;
                    case "colOff": r.Read();
                        ColumnOffset = new Text<long>(long.Parse(r.Value));
                        break;
                    case "row": r.Read();
                        Row = new Text<int>(int.Parse(r.Value));
                        break;
                    case "rowOff": r.Read();
                        RowOffset = new Text<long>(long.Parse(r.Value));
                        break;
                    default: break;
                }
            }
        }
    }

    public class Text<T> : XmlSerializable
    {
        [XmlText]
        public T Value { get; set; }

        public Text() { }

        public Text(T value)
        {
            Value = value;
        }
    }
}
